<?php
namespace Message\Controller;
use Common\Controller\MessageController;
class TaskController extends MessageController {

    public function index(){

        return 1;
        $info = M('song_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id')->where(array('status'=>'-1'))->group('touser')->order('wxopen_template_send_id desc')->select();

        foreach ($info as $key=>$value){

            $data = array(
                'touser'=>$value['touser'],
                'template_id'=>$value['template_id'],
                'page'=>$value['page'],
                'form_id'=>$value['form_id'],
            );
            $res = $this->sendtemplate($value['appid'],$value['secret'],$data);
            $save = array(
                'errcode'=>$res['errcode'],
                'errmsg'=>$res['errmsg'],
            );
            if ($res['errcode']==0){
                $save['status'] = 1;
                $save['send_time'] = time();
                M('song_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }else{
                $save['status'] = 2;
                M('song_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }

        }

    }

    public function songsend(){

        return 1;
        $info = M('song_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id,data,emphasis_keyword')->where(array('status'=>'-1'))->group('touser')->order('wxopen_template_send_id desc')->select();

        foreach ($info as $key=>$value){

            $data = array(
                'touser'=>$value['touser'],
                'template_id'=>$value['template_id'],
                'page'=>$value['page'],
                'form_id'=>$value['form_id'],
                'data'=>json_decode($value['data']),
                'emphasis_keyword'=>$value['emphasis_keyword'],
            );
            $res = $this->sendtemplate($value['appid'],$value['secret'],$data);
            $save = array(
                'errcode'=>$res['errcode'],
                'errmsg'=>$res['errmsg'],
            );
            if ($res['errcode']==0){
                $save['status'] = 1;
                $save['send_time'] = time();
                M('song_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }else{
                M('song_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }

        }


    }

    /**
     * 缓存到文件
     */
    private function cacheidiom(){

        $info = M('idiom_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id')->where(array('status'=>'-1'))->order('wxopen_template_send_id asc')->select();

        $data = array();
        foreach ($info as $key=>$value){

            $data[$value['touser']] = $value;

        }

        S('idiomcachetem',$data,7200);

    }

    /**
     * 成语的
     * @return int
     */
    public function sendidiom(){

        exit;
       // $info = M('idiom_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id')->where(array('status'=>'-1'))->limit(0,600)->group('touser')->order('wxopen_template_send_id desc')->select();

//        $wheret['touser'] = "o5_0244GNpPNXEopelGK75pSVByU";
//        $wheret['status'] = "-1";
//        $info = M('idiom_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id')->where($wheret)->limit(1)->order('wxopen_template_send_id desc')->select();

        $info = S('idiomcachetem');

        if (empty($info)){
            $this->cacheidiom();
            $info = S('idiomcachetem');
        }

        foreach ($info as $key=>$value){

            $fdata = array(
                'keyword1'=>array(
                    'value'=>'猜不出?看答案！',
                    'color'=>'#ff0000',
                ),
                'keyword2'=>array(
                    'value'=>'益智成语全新改版，猜不出的就看答案！',
                    'color'=>'#173177',
                ),
                'keyword3'=>array(
                    'value'=>'有奖活动策划中，敬请期待',
                    'color'=>'#173177',
                ),

            );
            $tem = "rB8aEaCXd1hDGOswKsCvhY4CKAz5Zg6dIz3MlbRQmwU";
            $data = array(
                'touser'=>$value['touser'],
                'template_id'=>$tem,
                'page'=>'pages/index/index',
                'form_id'=>$value['form_id'],
                'data'=>$fdata,
                'emphasis_keyword'=>'keyword1.DATA'
            );
            $res = $this->sendtemplate($value['appid'],$value['secret'],$data);
            $save = array(
                'errcode'=>$res['errcode'],
                'errmsg'=>$res['errmsg'],
                'data'=>json_encode($fdata),
                'emphasis_keyword'=>'keyword1.DATA',
            );
            echo $res['errcode'];
            if ($res['errcode']==0){
                $save['status'] = 1;
                $save['send_time'] = time();
                M('idiom_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }else{
                $save['status'] = 2;
                M('idiom_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }

        }

    }

    public function test5(){

        echo date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600));

    }

    /**
     * 猜成语通知
     * @return int
     */
    public function sendidiom2(){

        exit;
       $info = M('idiom_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id')->where(array('status'=>'-1'))->limit(0,500)->group('touser')->order('wxopen_template_send_id desc')->select();

//        $open = 'o5_0244GNpPNXEopelGK75pSVByU';
//        $where2['touser'] = $open;
//        $where2['status'] = '-1';
//        $info = M('idiom_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id')->where($where2)->limit(1)->order('wxopen_template_send_id desc')->select();


        $year = date('Y');
        $week = (int)date('W');

        $where = array(
            'week.year'=>$year,
            'week.week'=>$week,
        );

        $list =M()->table('mj_idiom_week as week')->join('mj_idiom_member as uesr on  week.idiom_member_id = uesr.idiom_member_id')->where($where)->field('week.getnum,week.idiom_week_id,uesr.nickName,uesr.avatarUrl,uesr.openId')->limit(100)->order('week.getnum desc' )->select();

        $myrank = 0;

        foreach ($info as $key=>$value){

            foreach ($list as $lkey=>$lval){

                if ($list[$lkey]['openid']==$value['touser']){
                    $myrank = $lkey+1;
                    $scoe = $list[$lkey]['getnum'];

                }

            }

            $myran = '排名'.$myrank.'得分'.$scoe;
            if ($myrank==0){
                $myran = '未上榜';
            }

            $fdata = array(
                'keyword1'=>array(
                    'value'=>$myran,
                    'color'=>'#173177',
                ),
                'keyword2'=>array(
                    'value'=>'开奖时间2018-5-20晚',
                    'color'=>'#173177',
                ),
                'keyword3'=>array(
                    'value'=>'每周进行答题积分累计，前十名可获得奖励。',
                    'color'=>'#173177',
                ),
                'keyword4'=>array(
                    'value'=>'第一名将获得200元，第二名获得100元，第三名获得50元，其他名次请点击规则查看。',
                    'color'=>'#173177',
                ),
                'keyword5'=>array(
                    'value'=>'现金大奖等你拿！',
                    'color'=>'#173177',
                ),
            );
            $temp = "auib7FiXhTov92l7erGkoymp9rNpcGX6bF-qrvBqVEk";
            $data = array(
                'touser'=>$value['touser'],
                'template_id'=>$temp,
                'page'=>'pages/index/index',
                'form_id'=>$value['form_id'],
                'data'=>$fdata,
                'emphasis_keyword'=>'keyword5.DATA'
            );
            $res = $this->sendtemplate($value['appid'],$value['secret'],$data);
            $save = array(
                'errcode'=>$res['errcode'],
                'errmsg'=>$res['errmsg'],
                'data'=>json_encode($fdata),
                'emphasis_keyword'=>'keyword2.DATA',
            );
            echo $res['errcode'];
            if ($res['errcode']==0){
                $save['status'] = 1;
                $save['send_time'] = time();
                M('idiom_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }else{
                $save['status'] = 2;
                M('idiom_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }

        }

    }





    public function tests(){

        $info = M('song_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id')->where(array('status'=>'-1'))->order('wxopen_template_send_id desc')->select();

        $data = array();
        foreach ($info as $key=>$value){

            $data[$value['touser']] = $value;

        }


       var_dump($data);

    }

    /**
     * 多线程，使用需谨慎
     */

    public function test(){

        return 1;
        error_reporting(E_ALL);

        $start_task = 1;

        $num = 2000;
        $start = (10*$num)*$start_task;

        $handle1 = popen('php index.php /Message/Task/sendidiom', 'r');
//        $handle2 = popen('php index.php /Message/Task/sendidiom', 'r');
//        $handle3 = popen('php index.php /Message/Task/sendidiom', 'r');
//        $handle4 = popen('php index.php /Message/Task/sendidiom', 'r');
//        $handle5 = popen('php index.php /Message/Task/sendidiom', 'r');
//        $handle6 = popen('php index.php /Message/Task/sendidiom', 'r');
//        $handle7 = popen('php index.php /Message/Task/sendidiom', 'r');
//        $handle8 = popen('php index.php /Message/Task/sendidiom', 'r');
//        $handle9 = popen('php index.php /Message/Task/sendidiom', 'r');
//        $handle10 = popen('php index.php /Message/Task/sendidiom', 'r');

        echo "'$handle1'; " . gettype($handle1) . "\n";
//        echo "'$handle2'; " . gettype($handle2) . "\n";
//        echo "'$handle3'; " . gettype($handle3) . "\n";
//        echo "'$handle4'; " . gettype($handle4) . "\n";
//        echo "'$handle5'; " . gettype($handle5) . "\n";
//        echo "'$handle6'; " . gettype($handle6) . "\n";
//        echo "'$handle7'; " . gettype($handle7) . "\n";
//        echo "'$handle8'; " . gettype($handle8) . "\n";
//        echo "'$handle9'; " . gettype($handle9) . "\n";
//        echo "'$handle10'; " . gettype($handle10) . "\n";

//sleep(20);
        while(!feof($handle1) || !feof($handle2) || !feof($handle3) || !feof($handle4) || !feof($handle5) || !feof($handle6) || !feof($handle7) || !feof($handle8) || !feof($handle9) || !feof($handle10) ){
            $read = fgets($handle1);
            echo $read;

            $read = fgets($handle2);
            echo $read;

            $read = fgets($handle3);
            echo $read;

            $read = fgets($handle4);
            echo $read;

            $read = fgets($handle5);
            echo $read;

            $read = fgets($handle6);
            echo $read;

            $read = fgets($handle7);
            echo $read;

            $read = fgets($handle8);
            echo $read;

            $read = fgets($handle9);
            echo $read;

            $read = fgets($handle10);
            echo $read;
        }


        pclose($handle1);
//        pclose($handle2);
//        pclose($handle3);
//        pclose($handle4);
//        pclose($handle5);
//        pclose($handle6);
//        pclose($handle7);
//        pclose($handle8);
//        pclose($handle9);
//        pclose($handle10);

    }

}